﻿using SCG.General;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SCG.SolarVengeanceEngine.StarShips
{
    public class Zealot : StarShip
    {
        //constructor
        public Zealot(SVGame game, Player owner, int engines, int influence, StarSystem origin)
            : base(game, owner, engines, influence, origin)
        {
        }

        //ship name
        public override string Name
        {
            get
            {
                return "Zealot";
            }
        }

        //ship type
        public override StarShipType ShipType
        {
            get
            {
                return StarShipType.Zealot;
            }
        }

        //Description
        public override string Description
        {
            get
            {
                return "Zealots are the agents of Star Lords that are driven by religious frenzy.  They can cause StarSystems to convert to their beliefs, and come under the control of the Zealot's owner. " +
                    "A Zealot exerts a sphere of Influence, and the closest StarSystem in that range has a 1% chance per Impulse of converting. " +
                    "Enemy Capitals have a much lower chance of converting, only 1 in 1,000 per Impulse. " +
                    "Bringing multiple Zealots to a StarSystem does not increase the conversion chance.";
            }
        }

        //randomize the radius color
        public override General.SCGColor RadiusColor
        {
            get
            {
                byte rg = (byte)(SVGame.RNG.Next(25) * 10);
                return new SCGColor(128, rg, rg, 255);
            }
        }

        //convert StarSystems
        protected internal override void PreMoveProcessing()
        {
            StarSystem closest = null;
            double closestDist = Double.MaxValue;
            foreach(StarSystem sys in Game.StarSystems)
            if (sys.Owner != Owner)
            {
                double dist = sys.Distance(this);
                if (dist <= Value)
                    if (dist < closestDist)
                        if (!sys.WasInfluencedBy(Owner))
                        {
                            closestDist = dist;
                            closest = sys;
                        }
            }
    
            if (closest != null)
            {        
                closest.Influence(Owner);
                bool converted = SVGame.RNG.Next(100) == 33;
                if (closest.IsCapital)
                    converted = SVGame.RNG.Next(1000) == 333;
                if (converted)
                {
                    //conquer, preserving shields
                    int shields = closest.Shields;
                    Game.ConquerStarSystem(Owner, closest);
                    closest.Shields = shields;
                    Game.SubmitStateChange(StateChange.SetShields, closest, shields);
                    if (Game.Host != null)
                        Game.Host.StarSystemConverted(closest, Owner);
                    Game.SubmitStateChange(StateChange.ConvertStarSystem, closest, Owner);
                }
            }
        }
    }
}
